﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _12_DataGridView
{
    public partial class Form1 : Form
    {
        //模拟产生的数据
        Random rd = new Random();

        //创建绑定数据源的对象
        BindingList<Product> products = new BindingList<Product>();

        public Form1()
        {
            InitializeComponent();
            //表格绑定数据源
            this.dataGridView1.DataSource = products;
            //禁止修改行的高度
            this.dataGridView1.AllowUserToResizeRows = false;
        }

        /// <summary>
        /// 增加一行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            //获取集合长度来确定编号
            int id = products.Count + 1;
            //产品名称
            string productName="";
            //所属产线ID
            int productLineID = 0;
            //根据不同的产线产生不同的产品信息
            switch (rd.Next(1,4))
            {
                case 1:
                    productName = "产品A";
                    productLineID = 1;
                    break;
                case 2:
                    productName = "产品B";
                    productLineID = 2;
                    break;
                case 3:
                    productName = "产品C";
                    productLineID = 3;
                    break;
            }
            //产品是否合格
            bool productQualified = rd.Next(0, 2) == 0 ? false : true;
            //添加到集合中
            products.Add(new Product(id, productName, productQualified, productLineID));
            //根据是否合格设置不同的字体颜色
            Color color = productQualified ? Color.Black : Color.Red;
            //设置新添加的行的字体颜色:this.dataGridView1.Rows.Count-1（最新添加的行索引）
            this.dataGridView1.Rows[this.dataGridView1.Rows.Count-1].DefaultCellStyle.ForeColor = color;
            //设置新添加的行为活跃行(确保最新添加的行可见)
            this.dataGridView1.CurrentCell = this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].Cells[0];

        }

        private void 添加ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Frm_EditorProduct frm = new Frm_EditorProduct();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                products.Add(frm.product);
                //根据是否合格设置不同的字体颜色
                Color color = frm.product.ProductQualified ? Color.Black : Color.Red;
                //设置新添加的行的字体颜色:this.dataGridView1.Rows.Count-1（最新添加的行索引）
                this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].DefaultCellStyle.ForeColor = color;
                //设置新添加的行为活跃行(确保最新添加的行可见)
                this.dataGridView1.CurrentCell = this.dataGridView1.Rows[this.dataGridView1.Rows.Count - 1].Cells[0];

            }
        }
        /// <summary>
        /// 移除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            //删除最后一行
            if (this.dataGridView1.Rows.Count > 0)
            {
                products.RemoveAt(this.dataGridView1.Rows.Count - 1);
            }
        }

        /// <summary>
        /// 修改一行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button3_Click(object sender, EventArgs e)
        {
            //获取当前选中的行索引
            int index = this.dataGridView1.CurrentRow.Index;
            //获取当前选中行的对象
            Product product = products[index];
            //窗体显示
            Frm_EditorProduct frm_EditorProduct = new Frm_EditorProduct(product);
            frm_EditorProduct.Text = "修改产品";
            if (frm_EditorProduct.ShowDialog()==DialogResult.OK)
            {
                //判断颜色是否合格
                Color color = frm_EditorProduct.product.ProductQualified ? Color.Black : Color.Red;
                //设置指定行的颜色
                this.dataGridView1.Rows[index].DefaultCellStyle.ForeColor = color;
                //设置指定行的数据
                products[index] = frm_EditorProduct.product;
            }
        }
        /// <summary>
        /// 删除选中
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button4_Click(object sender, EventArgs e)
        {
            if (products.Count>0)
            {
                //获取活跃的单元格的索引
                int index = this.dataGridView1.CurrentRow.Index;
                //删除选中的行
                products.RemoveAt(index);
            }
        }
    }
}
